package com.kedacom.demo.quartz2;

import java.net.InetAddress;
import java.util.UUID;

import org.quartz.SchedulerException;
import org.quartz.spi.InstanceIdGenerator;

/**
 * 解决集群环境为服务器生成ID时，linux获取主机IP时的bug
 * @author luocanfeng
 * @date 2017年4月24日
 */
public class MyInstanceIdGenerator implements InstanceIdGenerator {

	@Override
	public String generateInstanceId() throws SchedulerException {
		String instanceId = null;
		try {
			InetAddress localhost = InetAddress.getLocalHost();
			if (localhost != null) {
				instanceId = localhost.getHostName() + System.currentTimeMillis();
			}
		} catch (Exception e) {
		}
		return instanceId != null ? instanceId : randomString();
	}

	private static String randomString() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	public static void main(String[] args) {
		System.out.println(randomString());
	}

}
